import asyncio
from utils import os
from .info import get_playlist


async def download_video(url, folder: str=None, filename=None):
	if folder is None:
		folder = ""
	else:
		if not folder.endswith("/") and not folder.endswith("\\"):
			folder += "/"

	if filename is None:
		command = f'youtube-dl -o "{folder}%(title)s.%(ext)s" -x --audio-format mp3 {url}'
	else:
		command = f'youtube-dl -o "{folder}{filename}" -x --audio-format mp3 {url}'

	await os.system(command)

async def download_playlist(url, path) -> list:
	tasks = []
	playlist = await get_playlist(url, path)
	for video in playlist["videos"]:
		task = asyncio.create_task(download_video(video["id"], path))
		tasks.append(task)

	return tasks
